﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Oracle.ManagedDataAccess.Client;

namespace YJ.Data.ORACLE
{
    public class ProgramBuilderButtons : YJ.Data.Interface.IProgramBuilderButtons
    {
        private DBHelper dbHelper = new DBHelper();
        /// <summary>
        /// 构造函数
        /// </summary>
        public ProgramBuilderButtons()
        {
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.ProgramBuilderButtons实体类</param>
        /// <returns>操作所影响的行数</returns>
        public int Add(YJ.Data.Model.ProgramBuilderButtons model)
        {
            string sql = @"INSERT INTO ProgramBuilderButtons
				(ID,ProgramID,ButtonID,ButtonName,ClientScript,Ico,ShowType,Sort,IsValidateShow) 
				VALUES(:ID,:ProgramID,:ButtonID,:ButtonName,:ClientScript,:Ico,:ShowType,:Sort,:IsValidateShow)";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = model.ID },
				new OracleParameter(":ProgramID", OracleDbType.Varchar2){ Value = model.ProgramID },
				model.ButtonID == null ? new OracleParameter(":ButtonID", OracleDbType.Varchar2) { Value = DBNull.Value } : new OracleParameter(":ButtonID", OracleDbType.Varchar2) { Value = model.ButtonID },
				new OracleParameter(":ButtonName", OracleDbType.Varchar2){ Value = model.ButtonName },
				model.ClientScript == null ? new OracleParameter(":ClientScript", OracleDbType.Varchar2) { Value = DBNull.Value } : new OracleParameter(":ClientScript", OracleDbType.Varchar2) { Value = model.ClientScript },
				model.Ico == null ? new OracleParameter(":Ico", OracleDbType.Varchar2) { Value = DBNull.Value } : new OracleParameter(":Ico", OracleDbType.Varchar2) { Value = model.Ico },
				new OracleParameter(":ShowType", OracleDbType.Int32){ Value = model.ShowType },
				new OracleParameter(":Sort", OracleDbType.Int32){ Value = model.Sort },
				new OracleParameter(":IsValidateShow", OracleDbType.Int32){ Value = model.IsValidateShow }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.ProgramBuilderButtons实体类</param>
        public int Update(YJ.Data.Model.ProgramBuilderButtons model)
        {
            string sql = @"UPDATE ProgramBuilderButtons SET 
				ProgramID=:ProgramID,ButtonID=:ButtonID,ButtonName=:ButtonName,ClientScript=:ClientScript,Ico=:Ico,ShowType=:ShowType,Sort=:Sort,IsValidateShow=:IsValidateShow
				WHERE ID=:ID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ProgramID", OracleDbType.Varchar2){ Value = model.ProgramID },
				model.ButtonID == null ? new OracleParameter(":ButtonID", OracleDbType.Varchar2) { Value = DBNull.Value } : new OracleParameter(":ButtonID", OracleDbType.Varchar2) { Value = model.ButtonID },
				new OracleParameter(":ButtonName", OracleDbType.Varchar2){ Value = model.ButtonName },
				model.ClientScript == null ? new OracleParameter(":ClientScript", OracleDbType.Varchar2) { Value = DBNull.Value } : new OracleParameter(":ClientScript", OracleDbType.Varchar2) { Value = model.ClientScript },
				model.Ico == null ? new OracleParameter(":Ico", OracleDbType.Varchar2) { Value = DBNull.Value } : new OracleParameter(":Ico", OracleDbType.Varchar2) { Value = model.Ico },
				new OracleParameter(":ShowType", OracleDbType.Int32){ Value = model.ShowType },
				new OracleParameter(":Sort", OracleDbType.Int32){ Value = model.Sort },
				new OracleParameter(":IsValidateShow", OracleDbType.Int32){ Value = model.IsValidateShow },
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = model.ID }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        public int Delete(Guid id)
        {
            string sql = "DELETE FROM ProgramBuilderButtons WHERE ID=:ID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = id }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 将DataRedar转换为List
        /// </summary>
        private List<YJ.Data.Model.ProgramBuilderButtons> DataReaderToList(OracleDataReader dataReader)
        {
            List<YJ.Data.Model.ProgramBuilderButtons> List = new List<YJ.Data.Model.ProgramBuilderButtons>();
            YJ.Data.Model.ProgramBuilderButtons model = null;
            while (dataReader.Read())
            {
                model = new YJ.Data.Model.ProgramBuilderButtons();
                model.ID = dataReader.GetString(0).ToGuid();
                model.ProgramID = dataReader.GetString(1).ToGuid();
                if (!dataReader.IsDBNull(2))
                    model.ButtonID = dataReader.GetString(2).ToGuid();
                model.ButtonName = dataReader.GetString(3);
                if (!dataReader.IsDBNull(4))
                    model.ClientScript = dataReader.GetString(4);
                if (!dataReader.IsDBNull(5))
                    model.Ico = dataReader.GetString(5);
                model.ShowType = dataReader.GetInt32(6);
                model.Sort = dataReader.GetInt32(7);
                model.IsValidateShow = dataReader.GetInt32(8);
                List.Add(model);
            }
            return List;
        }
        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.ProgramBuilderButtons> GetAll()
        {
            string sql = "SELECT * FROM ProgramBuilderButtons";
            OracleDataReader dataReader = dbHelper.GetDataReader(sql);
            List<YJ.Data.Model.ProgramBuilderButtons> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
        /// <summary>
        /// 查询记录数
        /// </summary>
        public long GetCount()
        {
            string sql = "SELECT COUNT(*) FROM ProgramBuilderButtons";
            long count;
            return long.TryParse(dbHelper.GetFieldValue(sql), out count) ? count : 0;
        }
        /// <summary>
        /// 根据主键查询一条记录
        /// </summary>
        public YJ.Data.Model.ProgramBuilderButtons Get(Guid id)
        {
            string sql = "SELECT * FROM ProgramBuilderButtons WHERE ID=:ID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ID", OracleDbType.Varchar2){ Value = id }
			};
            OracleDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.ProgramBuilderButtons> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List.Count > 0 ? List[0] : null;
        }

        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.ProgramBuilderButtons> GetAll(Guid id)
        {
            string sql = "SELECT * FROM ProgramBuilderButtons WHERE ProgramID=:ProgramID";
            OracleParameter[] parameters = new OracleParameter[]{
				new OracleParameter(":ProgramID", OracleDbType.Varchar2){ Value = id }
			};
            OracleDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.ProgramBuilderButtons> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
    }
}